﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace QLTV_GiaoDien
{
    public partial class uc_QuyenHan : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_QuyenHan()
        {
            InitializeComponent();
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void enableTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled=false;
            }
            foreach (Control i in ctr.Controls)
                enableTextFalse(i);
        }
        QuyenHan_BLL quyenHan_bll = new QuyenHan_BLL();
        int flag;
        private void uc_QuyenHan_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = quyenHan_bll.layDuLieu();
            panelControl1.Hide();
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 0;
            txt_MaQuyen.Enabled = false;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Xem.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            panelControl1.Show();
        }
        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            TrungGian tg = new TrungGian();
            flag = 1;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Xem.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            txt_MaQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Quyền").ToString();
            tg.XXX=txt_TenQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            txt_GhiChu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            panelControl1.Show();
        }
        private void bbi_Xem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            flag = 2;
            txt_MaQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Quyền").ToString();
            tg.XXX=txt_TenQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            txt_GhiChu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            panelControl1.Show();
            enableTextFalse(this);
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            panelControl1.Hide();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
            QuyenHan_Public q = new QuyenHan_Public();

            if (flag == 0)
            {
                //them
                q.TenQuyen = txt_TenQuyen.Text;
                q.GhiChu = txt_GhiChu.Text;
                quyenHan_bll.insert_QuyenHan(q,q);
                //
                
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm mới Quyền: '"+txt_TenQuyen.Text+"'";
                nhatKy_bll.insert_nhatKy(nk,nk,nk,nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag == 1)
            {//sua

                q.MaQuyen = int.Parse(txt_MaQuyen.Text);
                q.TenQuyen = txt_TenQuyen.Text;
                q.GhiChu = txt_GhiChu.Text;
                quyenHan_bll.Update_QuyenHan(q,q,q);
                //
                
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Quyền: '"+tg.XXX+"' thành: '" + txt_TenQuyen.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            if (flag == 2)
            {//xem
                q.MaQuyen = int.Parse(txt_MaQuyen.Text);
                q.TenQuyen = txt_TenQuyen.Text;
                q.GhiChu = txt_GhiChu.Text;
                quyenHan_bll.Update_QuyenHan(q, q, q);
                //
                
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Quyền: '" + tg.XXX + "' thành: '" + txt_TenQuyen.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                
            }
            uc_QuyenHan_Load(sender,e);
            panelControl1.Show();
            Huy_Text(this);
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
        }

        private void bbi_Refresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            uc_QuyenHan_Load(sender, e);
        }

        private void bbi_Xoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            QuyenHan_Public q = new QuyenHan_Public();
            q.MaQuyen = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Quyền").ToString());
            q.TenQuyen = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            if (XtraMessageBox.Show("Bạn có muốn xóa Tên quyền: '" + q.TenQuyen + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //
           
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Xóa";
                nk.MoTa = "Xóa thông tin Quyền: '"+q.TenQuyen+"'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                quyenHan_bll.delete_QuyenHan(q);
                uc_QuyenHan_Load(sender,e);
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
           
            txt_MaQuyen.Text =gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Quyền").ToString();
            tg.XXX=txt_TenQuyen.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Quyền").ToString();
            txt_GhiChu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Ghi Chú").ToString();
            panelControl1.Show();
        }

        private void txt_MaQuyen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }


    }
}
